package platform.database;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import framework.sepal4java.common.struct.ConstantEnum.CompareType;

public class Condition {
    private String column;
    private CompareType compare;
    private Object value;
    private JSONObject jo = null;
    private String json = null;

    public Condition(String column, CompareType compare, Object value) {
        this.column = column;
        this.compare = compare;
        this.value = value;
    }

    public Condition(String column, String compare, Object value) {
        this.column = column;
        this.compare = CompareType.fromString(compare);
        this.value = value;
    }

    public Condition(String condition) {
        try {
            jo = JSON.parseObject(condition);
            this.json = condition;
            column = jo.getString("column");
            compare = CompareType.fromString(jo.getString("compare"));
            value = jo.get("value");
        } catch (Exception e) {
            String[] strs = condition.split(" ");
            column = strs[0];
            compare = CompareType.fromString(strs[1]);
            try {
                value = Integer.valueOf(strs[2]);
            } catch (Exception e1) {
                try {
                    value = Double.valueOf(strs[2]);
                } catch (Exception e2) {
                    if (strs[2].equalsIgnoreCase("true")) {
                        value = true;
                    } else if (strs[2].equalsIgnoreCase("false")) {
                        value = false;
                    } else {
                        value = strs[2];
                    }
                }
            }
        }
    }

    public Condition(JSONObject jo) {
        this.jo = jo;
        column = jo.getString("column");
        compare = CompareType.fromString(jo.getString("compare"));
        value = jo.get("value");
    }

    boolean check(Object value) {
        switch (compare) {
            case EQ:
                return this.value.equals(value);
            case NE:
                return !this.value.equals(value);
            case GT:
                return ((Comparable) value).compareTo(this.value) > 0;
            case GE:
                return ((Comparable) value).compareTo(this.value) >= 0;
            case LT:
                return ((Comparable) value).compareTo(this.value) < 0;
            case LE:
                return ((Comparable) value).compareTo(this.value) <= 0;
            case IN:
                String[] strs = ((String) value).split(",");
                for (String str : strs) {
                    Object obj;
                    try {
                        obj = Integer.valueOf(str);
                    } catch (Exception e) {
                        try {
                            obj = Double.valueOf(str);
                        } catch (Exception e1) {
                            if (str.equalsIgnoreCase("true")) {
                                obj = true;
                            } else if (str.equalsIgnoreCase("false")) {
                                obj = false;
                            } else {
                                obj = str;
                            }
                        }
                    }
                    if (obj.equals(this.value)) {
                        return true;
                    }
                }
            default:
                return false;
        }
    }

    public String toJSONString() {
        if (json == null) {
            jo = new JSONObject(3);
            jo.put("column", column);
            jo.put("compare", compare);
            jo.put("value", value);
            json = jo.toJSONString();
        }
        return json;
    }

    public JSONObject toJSONObject() {
        if (jo == null) {
            jo = new JSONObject(3);
            jo.put("column", column);
            jo.put("compare", compare);
            jo.put("value", value);
        }
        return jo;
    }

    @Override
    public String toString() {
        return toJSONString();
    }

    public String getColumn() {
        return column;
    }

    public CompareType getCompare() {
        return compare;
    }

    public Object getValue() {
        return value;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public void setCompare(CompareType compare) {
        this.compare = compare;
    }

    public void setValue(Object value) {
        this.value = value;
    }
}
